package com.whc.config;

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.whc.interceptor.JwtTokenUserInterceptor;
import com.whc.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.List;

/**
 * @author 韦红材
 * @ClassName WebMvcConfiguration
 * @data 2024/10/22 14:12
 * @Version V1.0
 **/
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {


//    //注册自定义拦截器
//
//    protected void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(jwtTokenUserInterceptor)
//                .addPathPatterns("/client/**")
//                .excludePathPatterns("/client/user/login","/client/user/register");
//    }
    //通过knife4j生成接口文档
    @Bean
    public Docket docket1(){
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("第一个项目接口文档")
                .version("2.0")
                .description("第一个项目接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("用户端接口")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.whc.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    //添加静态资源
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    //添加消息转化器
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        log.info("扩展消息转化器");
        //创建一个消息转换器对象
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //设置一个对象转化器，用前面自定义的json转化器，包含序列化和反序列化
        converter.setObjectMapper(new JacksonObjectMapper());

        //讲这个转化器添加到容器当中
        converters.add(0,converter);
    }
}
